#include "dynamic_widget.h"
#include <QHBoxLayout>
#include <QtDebug>
#include <QTableWidgetItem>
DynamicWidget::DynamicWidget(QWidget *parent)
    : QWidget{parent}
    , tableWidget(new QTableWidget)
{
    initUI();
}

void DynamicWidget::updataData(ELFRead *elfRead, int index)
{
    tableWidget->clear();
    Elf64_Shdr shdr = elfRead->getSection(index);
    Elf64_Dyn *dyntable = (Elf64_Dyn *)(elfRead->getBuffer() + shdr.sh_offset);
    tableWidget->setRowCount(shdr.sh_size /sizeof(Elf64_Dyn));
    tableWidget->setColumnCount(4);
    for(int i = 0 ; i < shdr.sh_size /sizeof(Elf64_Dyn);i++){
        Elf64_Dyn item = dyntable[i];
        QTableWidgetItem *itemId = new QTableWidgetItem(QString::number(i));
        QTableWidgetItem *itemType = new QTableWidgetItem(getDynType(item.d_tag));
        QTableWidgetItem *itemValue = new QTableWidgetItem(QString::number(item.d_un.d_val));
        QTableWidgetItem *itemPtr = new QTableWidgetItem(QString::number(item.d_un.d_val));
        tableWidget->setItem(i,0,itemId);
        tableWidget->setItem(i,1,itemType);
        tableWidget->setItem(i,2,itemValue);
        tableWidget->setItem(i,3,itemPtr);
    }
}

void DynamicWidget::initUI()
{
    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->addWidget(tableWidget);
    setLayout(mainLayout);
}
#define print_case_type(XX) case DT_##XX:return #XX;
QString DynamicWidget::getDynType(int64_t type)
{
    switch(type){

        print_case_type(NULL)
        print_case_type(NEEDED)
        print_case_type(PLTRELSZ)
        print_case_type(PLTGOT)
        print_case_type(HASH)
        print_case_type(STRTAB)
        print_case_type(SYMTAB)
        print_case_type(RELA)
        print_case_type(RELASZ)
        print_case_type(RELAENT	)
        print_case_type(STRSZ	)
        print_case_type(SYMENT	)
        print_case_type(INIT	)
        print_case_type(FINI	)
        print_case_type(SONAME)
        print_case_type(RPATH)
        print_case_type(SYMBOLIC)
        print_case_type(REL		)
        print_case_type(RELSZ	)
        print_case_type(RELENT	)
        print_case_type(PLTREL	)
        print_case_type(DEBUG	)
        print_case_type(TEXTREL	)
        print_case_type(JMPREL	)
        print_case_type(BIND_NOW)
        print_case_type(INIT_ARRAY)
        print_case_type(FINI_ARRAY	)
        print_case_type(INIT_ARRAYSZ	)
        print_case_type(FINI_ARRAYSZ	)
        print_case_type(RUNPATH	)
        print_case_type(FLAGS	)
        print_case_type(ENCODING)
        print_case_type(PREINIT_ARRAYSZ)
        print_case_type(SYMTAB_SHNDX)
        print_case_type(NUM)
        default:
            qInfo()<< type;
        return "";
    }
}
